package com.yaya.common.util.verify;

import lombok.Data;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 验证码工具类
 */

public class VerifyUtil {
    //验证码字符集
    //final 表明数组一旦被初始化之后，其引用就不能再被改变。
    private static final char[] chars = {
        '1', '2','V', 'M', 'N', 'O', 'P','b', 'c', 'd', 'e', 'f', 'g', 'h', 'Q', 'R', 'S', 'T', '3', '4', 'b', 'c', 'd', 'e', 'f', 'g', 'h','5', '6', '7', 'V', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T','8', '9','j', 'k', 'v', 'm', 'n', 'o', 'p', 'q',
        'a', 'b', '1', '2', 'c', 'd', 'D', 'E', 'F', '1', '2', 'G', 'H','e', 'f', 'g', 'h', 'u',  'r', 's', 't','1', '2','V', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
            'A', 'B', 'C', 'D', 'E',  '1', '2','F', 'G', 'H', 'U', 'J', 'K', 'V', 'M', 'N', 'O', 'b', 'c', 'd', 'e', 'f', 'g', 'h','P', 'Q', 'R', 'S', 'T',
            'a', 'b', 'c', 'd','V', 'M', 'N', 'O', 'P',  '1', '2','Q','b', 'c', 'd', 'e', 'f', 'g', 'h', 'R', 'S', 'T', 'e', 'f', 'g', 'h', 'u', 'j', 'k',
    };

    //字符数量
    private final Integer SIZE;

    //干扰线数量
    private final int LINES;

    //宽度
    private final int WIDTH;

    //高度
    private final int HEIGHT;

    //字体大小
    private final int FONT_SIZE;

    //字体倾斜
    private final Boolean TILT;

    //背景颜色
    private final Color BACKGROUND_COLOR;

    /**
     * 构建者
     */



//    private VerifyUtil(Builder builder){
//        int size = builder.size;
//        int lines = builder.lines;
//        int width = builder.width;
//        int height = builder.height;
//        int fontSize = builder.font_size;
//        Color backgroundColor = builder.background_color;
//        Boolean tilt = builder.tilt;
//    }

    private VerifyUtil(Builder builder){
        SIZE = builder.size;
        LINES = builder.lines;
        WIDTH = builder.width;
        HEIGHT = builder.height;
        FONT_SIZE = builder.font_size;
        BACKGROUND_COLOR = builder.background_color;
        TILT = builder.tilt;
    }



    /**
     * 实例化构造器
     *
     */
   public static Builder newBuilder(){
        return new Builder();
    }

    /**
     * 验证码生成
     */
    public Object[] createImage(){
        StringBuffer stringBuffer = new StringBuffer();
        BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);
        Graphics2D graphics = bufferedImage.createGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        graphics.setColor(BACKGROUND_COLOR);
        graphics.fillRect(0,0,WIDTH,HEIGHT);
        //随机字符
        Random random = new Random();
        int With = WIDTH /(SIZE + 1);
        int y = HEIGHT * 3/4;
        for (int i = 0; i < SIZE; i++){
            graphics.setColor(getColor());
            Font font = new Font(null,Font.ITALIC,FONT_SIZE);
            if (TILT){
                int nextInt = random.nextInt(45);
                nextInt = (random.nextBoolean() == true) ? nextInt: -nextInt;
                AffineTransform affineTransform = new AffineTransform();
                affineTransform.rotate(Math.toRadians(nextInt),0,0);
                font = font.deriveFont(affineTransform);
            }
            graphics.setFont(font);
            int x = (i * With) + (With /2);
            int nextInt = random.nextInt(chars.length);
            String CodeValue = String.valueOf(chars[nextInt]);
            graphics.drawString(CodeValue,x,y);
            stringBuffer.append(CodeValue);

        }
        for (int i = 0 ; i < LINES;i++){
            graphics.setColor(getColor());
            graphics.drawLine(random.nextInt(WIDTH),random.nextInt(HEIGHT),random.nextInt(WIDTH),random.nextInt(HEIGHT));
        }
        return new Object[]{stringBuffer.toString(),bufferedImage};

    }

    private Color getColor(){
        Random random = new Random();
        Color color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
        return color;
    }



    /**
     * 构造器对象
     */
    @Data
    public static class Builder{
        private int size = 4;

        private int lines = 10;


        private int width = 25;


        private int height = 10;


        private int font_size = 17;


        private Boolean tilt = true;


        private Color background_color = Color.PINK;
    }


}
